package com.jifeng.sterilizer.token.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;

/**
 * @author Administrator
 * @version 1.0
 **/
@Configuration
@EnableResourceServer
public class ResouceServerConfig extends ResourceServerConfigurerAdapter {


    public static final String RESOURCE_ID = "api";

    @Autowired
    TokenStore tokenStore;

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) {
        resources.resourceId(RESOURCE_ID)//资源 id
                .tokenStore(tokenStore)
//                .tokenServices(tokenService())//验证令牌的服务
                .stateless(true);
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {

//        http
//                .authorizeRequests()
//                .antMatchers("/**").access("#oauth2.hasScope('all')")
//                .and().csrf().disable()
//                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        http
                .authorizeRequests()
                .antMatchers("/sterilizer/user/register").permitAll()
                .antMatchers("/sterilizer/controller/update").permitAll()
                .antMatchers("/sterilizer/controller/save").permitAll()
                .antMatchers("/sterilizer/device/update").permitAll()
                .antMatchers("/sterilizer/device/save").permitAll()
                .antMatchers("/sterilizer/sms/vcode").permitAll()
                .antMatchers("/sterilizer/farm/list").permitAll()
                .antMatchers("/sterilizer/user/resetPassword").permitAll()
//                .antMatchers("/sterilizer/user/pc/**").permitAll()
                .antMatchers("/sterilizer/user/pc/login").permitAll()
//                .antMatchers("/sterilizer/farmuser/pc/**").permitAll()
                .antMatchers("/sterilizer/home/pc/**").permitAll()
//                .antMatchers("/sterilizer/liquid/pc/**").permitAll()
//                .antMatchers("/sterilizer/liquidtype/pc/**").permitAll()
//                .antMatchers("/sterilizer/devicetype/pc/**").permitAll()
//                .antMatchers("/sterilizer/device/pc/**").permitAll()
//                .antMatchers("/sterilizer/vwdevice/pc/**").permitAll()
//                .antMatchers("/sterilizer/farm/pc/**").permitAll()
//                .antMatchers("/sterilizer/vmcontroller/pc/**").permitAll()
//                .antMatchers("/sterilizer/controllertype/pc/**").permitAll()
//                .antMatchers("/sterilizer/controller/pc/**").permitAll()
//                .antMatchers("/sterilizer/phoneuser/pc/**").permitAll()
                .antMatchers("/sterilizer/**").access("#oauth2.hasScope('all')")
                .antMatchers("/**").permitAll()
//                .antMatchers("/sterilizer/farm/**").access("#oauth2.hasScope('all')")
                .and().csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
//        http
//                .authorizeRequests()
//                .antMatchers("/sterilizer/farm/list").permitAll();
    }

    //资源服务令牌解析服务
//    @Bean
//    public ResourceServerTokenServices tokenService() {
//        //使用远程服务请求授权服务器校验token,必须指定校验token 的url、client_id，client_secret
//        RemoteTokenServices service=new RemoteTokenServices();
//        service.setCheckTokenEndpointUrl("http://localhost:8080/oauth/check_token");
//        service.setClientId("c1");
//        service.setClientSecret("secret");
//        return service;
//    }
}
